package cn.schoolwow.ams.module.block.form.initial.flow;

import cn.schoolwow.ams.domain.block.common.BlockOption;
import cn.schoolwow.ams.domain.block.form.FormBlockOption;
import cn.schoolwow.ams.module.block.common.domain.BlockContext;
import cn.schoolwow.ams.module.block.common.flow.SetBlockContextFieldValueFlow;
import cn.schoolwow.ams.module.block.common.flow.SetBlockOperationCommonFlow;
import cn.schoolwow.ams.module.block.form.initial.domain.FormBlockContext;
import cn.schoolwow.ams.module.block.listBlock.clazzBlock.initial.flow.field.SetClazzListBlockFieldsFlow;
import cn.schoolwow.ams.module.initial.domain.InitialAMSContext;
import cn.schoolwow.quickflow.domain.FlowContext;
import cn.schoolwow.quickflow.flow.BusinessFlow;

import java.util.ArrayList;
import java.util.List;

public class SetFormBlockContextListFlow implements BusinessFlow {
    @Override
    public void executeBusinessFlow(FlowContext flowContext) throws Exception {
        List<FormBlockOption> formBlockOptionList = (List<FormBlockOption>) flowContext.checkData("formBlockOptionList");
        InitialAMSContext initialAMSContext = flowContext.checkInstanceData(InitialAMSContext.class);

        List<FormBlockContext> formBlockContextList = new ArrayList<>();
        for(FormBlockOption formBlockOption:formBlockOptionList){
            FormBlockContext formBlockContext = new FormBlockContext();

            flowContext.putTemporaryInstanceData(formBlockOption)
                    .putTemporaryInstanceData(formBlockContext)
                    .putTemporaryInstanceData(formBlockOption, BlockOption.class)
                    .putTemporaryInstanceData(formBlockContext, BlockContext.class);
            flowContext.executeFlowList(new SetBlockContextFieldValueFlow());
            setResponseFieldsFlow(flowContext);
            copyOption(flowContext);

            formBlockContextList.add(formBlockContext);
            initialAMSContext.formBlockContextList.add(formBlockContext);
        }
    }

    @Override
    public String name() {
        return "设置FormBlockContext列表";
    }

    private void setResponseFieldsFlow(FlowContext flowContext){
        FormBlockOption formBlockOption = flowContext.checkInstanceData(FormBlockOption.class);
        FormBlockContext formBlockContext = flowContext.checkInstanceData(FormBlockContext.class);

        flowContext.startFlow(new SetClazzListBlockFieldsFlow())
                .putCurrentCompositeFlowData("entityClass", formBlockOption.formClass)
                .putCurrentCompositeFlowData("amsFieldOptionList", formBlockContext.responseFields)
                .execute();
    }

    private void copyOption(FlowContext flowContext){
        FormBlockOption formBlockOption = flowContext.checkInstanceData(FormBlockOption.class);
        FormBlockContext formBlockContext = flowContext.checkInstanceData(FormBlockContext.class);

        formBlockContext.formClass = formBlockOption.formClass;
        formBlockContext.apiPrefix = "formBlock/"+formBlockContext.projectName+"/"+formBlockContext.formClass.getSimpleName();
        formBlockContext.remoteSelectList.addAll(formBlockOption.remoteSelectList);
        flowContext.startFlow(new SetBlockOperationCommonFlow())
                .putCurrentCompositeFlowData("blockOperationList", formBlockOption.formBlockHandlerBlockOperationList)
                .putCurrentCompositeFlowData("blockOperationContextList", formBlockContext.blockOperationContextList)
                .execute();
    }
}
